package cn.assumejson.springframework.core.io;

import cn.assumejson.springframework.util.ResourceUtils;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.URLUtil;

import java.io.IOException;
import java.net.URL;

/**
 * @author Mr. Li
 * @version 1.0
 * @since 1.0 2021/11/30 12:41
 */
public class DefaultResourceLoader implements ResourceLoader {

    public static final String ALL_CONFIGURATION_PROPERTIES = "classpath:*.properties";

    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not be null");
        if (location.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(location);
        }
        try {
            URL url = new URL(location);
            return new UrlResource(url);
        } catch (IOException e) {
            return new FileSystemResource(location);
        }
    }
}
